package com.zd.zjfx.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;

import org.apache.commons.collections.CollectionUtils;
import org.apache.http.Header;
import org.apache.http.HttpConnection;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Value;

public class Httptest {

	public static boolean getDayValue() throws Exception {

		
		return true;
	}

	public static final HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
		public boolean verify(String hostname, SSLSession session) {
			return true;
		}
	};

	public static String interfaceUntil(String path, String data, String jrdwptbh, String sydwptbh) {

		try {
//			HttpHeaders headers = new HttpHeaders();
//			headers.add("fwbm",""); // 添加服务编码
//	        headers.add("jrdwptbh",""); // 添加接入单位平台编号
//	        headers.add("sydwptbh",""); // 添加使用单位平台编号
//	        headers.add("nlbm",""); // 添加能力编码
//	        headers.add("ylbm",""); // 添加用例编码
//	        headers.add("access_signature",""); // 添加访问签名
//	        headers.add("sxcsbz",""); // 添加沙箱测试标志
			SSLContext sslContext = SSLContext.getInstance("SSL");
			sslContext.init(null, new TrustManager[] { new TrustAllManager() }, null);
			HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
			StringBuffer stringBuffer = new StringBuffer();
			String infos [] =path.split("/");
			URL url = new URL(path);
			HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
			//判断用户输入的地址是不是以/结尾
			if(infos[infos.length-3].charAt(0)=='2') {
				conn.setRequestProperty("fwbm", infos[infos.length-2]);
				conn.setRequestProperty("nlbm", infos[infos.length-3]);
				System.out.println(infos[infos.length-2]+infos[infos.length-3]);
			}else {
				conn.setRequestProperty("fwbm", infos[infos.length-1]);
				conn.setRequestProperty("nlbm", infos[infos.length-2]);
				System.out.println(infos[infos.length-1]+infos[infos.length-2]);
			}
			//能力编码202007对应的用例编码是911001,203066对应101501,其他的能力编码和用例编码一样
			if(conn.getRequestProperty("nlbm").equals("202007")) {
				conn.setRequestProperty("ylbm", "911001");
			} else if(conn.getRequestProperty("nlbm").equals("203066")) {
				conn.setRequestProperty("ylbm", "101501");
			} else {
				conn.setRequestProperty("ylbm", conn.getRequestProperty("nlbm"));
			}
			conn.setRequestProperty("jrdwptbh", jrdwptbh);
			conn.setRequestProperty("sydwptbh", sydwptbh);
			//conn.setRequestProperty("sxcsbz", "1");
			HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
			conn.setHostnameVerifier(new HostnameVerifier() {
				public boolean verify(String hostname, SSLSession session) {
					return true;
				}
			});
			// 设置连接输出流为true,默认false (post请求是以流的方式隐式的传递参数)
			conn.setDoOutput(true);
			// 设置连接输入流为true
			conn.setDoInput(true);
			// 设置请求方式为post
			conn.setRequestMethod("POST");
			// post请求缓存设为false
			conn.setUseCaches(false);
			conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
			OutputStream out = conn.getOutputStream();
			out.write(data.getBytes());
			out.flush();
			InputStream is = conn.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String str = "";
			while ((str = br.readLine()) != null) {
				stringBuffer.append(str);
			}
			is.close();
			conn.disconnect();
			System.out.println("完整结束");
			return stringBuffer.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public static String shaxiang(String path, String data, String jrdwptbh, String sydwptbh, Map<String, Object> headersMap) {

		try {
//			HttpHeaders headers = new HttpHeaders();
//			headers.add("fwbm",""); // 添加服务编码
//	        headers.add("jrdwptbh",""); // 添加接入单位平台编号
//	        headers.add("sydwptbh",""); // 添加使用单位平台编号
//	        headers.add("nlbm",""); // 添加能力编码
//	        headers.add("ylbm",""); // 添加用例编码
//	        headers.add("access_signature",""); // 添加访问签名
//	        headers.add("sxcsbz",""); // 添加沙箱测试标志
			SSLContext sslContext = SSLContext.getInstance("SSL");
			sslContext.init(null, new TrustManager[] { new TrustAllManager() }, null);
			HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
			StringBuffer stringBuffer = new StringBuffer();
			String infos [] =path.split("/");
			URL url = new URL(path);
			HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
			if (CollectionUtils.isEmpty(Collections.singleton(headersMap))) {
				//判断用户输入的地址是不是以/结尾
				if(infos[infos.length-3].charAt(0)=='2') {
					conn.setRequestProperty("fwbm", infos[infos.length-2]);
					conn.setRequestProperty("nlbm", infos[infos.length-3]);
					System.out.println(infos[infos.length-2]+infos[infos.length-3]);
				}else {
					conn.setRequestProperty("fwbm", infos[infos.length-1]);
					conn.setRequestProperty("nlbm", infos[infos.length-2]);
					System.out.println(infos[infos.length-1]+infos[infos.length-2]);
				}
				//能力编码202007对应的用例编码是911001,203066对应101501,其他的能力编码和用例编码一样
				if(conn.getRequestProperty("nlbm").equals("202007")) {
					conn.setRequestProperty("ylbm", "911001");
				} else if(conn.getRequestProperty("nlbm").equals("203066")) {
					conn.setRequestProperty("ylbm", "101501");
				} else {
					conn.setRequestProperty("ylbm", conn.getRequestProperty("nlbm"));
				}
				conn.setRequestProperty("jrdwptbh", jrdwptbh);
				conn.setRequestProperty("sydwptbh", sydwptbh);
				conn.setRequestProperty("sxcsbz", "1");
			} else {
				headersMap.forEach((key, value) -> {
					conn.setRequestProperty(key, (String) value);
				});
			}

			conn.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
			conn.setHostnameVerifier(new HostnameVerifier() {
				public boolean verify(String hostname, SSLSession session) {
					return true;
				}
			});
			// 设置连接输出流为true,默认false (post请求是以流的方式隐式的传递参数)
			conn.setDoOutput(true);
			// 设置连接输入流为true
			conn.setDoInput(true);
			// 设置请求方式为post
			conn.setRequestMethod("POST");
			// post请求缓存设为false
			conn.setUseCaches(false);
			conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
			OutputStream out = conn.getOutputStream();
			out.write(data.getBytes());
			out.flush();
			InputStream is = conn.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String str = "";
			while ((str = br.readLine()) != null) {
				stringBuffer.append(str);
			}
			is.close();
			conn.disconnect();
			System.out.println("完整结束");
			return stringBuffer.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	public static void main(String[] args) throws Exception {// 程序主函数
		// getDayValue();
		interfaceUntil("https://lqpt.chinatax.gov.cn:8443/access/sandbox/v2/invoke/202007/CXNSRJBXX/",
				Lq_Security.encryptEcb("9904033e1837094cfe11772fc2d90011", "{\"nsrsbh\":\"911305005809845195\"}"),"","");

	}

}
